import Vue from 'vue'
import axios from 'axios'

const api = axios.create({
  // baseURL: 'http://192.168.0.2:8081',
  baseURL: 'http://localhost:8081/',
  timeout: 5000
})

// eslint-disable-next-line no-unused-vars
let loading
// 请求
api.interceptors.request.use(config => {
  config.headers = {
    'Content-Type': 'application/json'
    // 'Content-Type': 'text/plain'

  }
  loading = Vue.prototype.$loading({
    lock: true,
    text: 'Loading',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.8)'
  })
  return config
}, err => {
  return Promise.reject(err)
})
// 响应
api.interceptors.response.use(res => {
  if (res.config.url === '/getNextFault') {
    setTimeout(() => {
      loading.close()
    }, 1000)
    return res
  } else {
    loading.close()
    return res
  }
}, err => {
  Vue.prototype.$message({
    type: 'error',
    message: '出现错误'
  })
  loading.close()
  return Promise.reject(err)
})

export default api

// 以上未连接后台数据，暂时不发请求，请求模块注释掉了
